/*
 * ContactDaoImpl.java
 *
 * Created on September 27, 2008, 5:53 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.vacuela.publishmx.dal;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.vacuela.publishmx.domains.Contact;
import org.vacuela.publishmx.domains.ContactNumber;
import org.springframework.transaction.annotation.Transactional;
/**
 *
 * @author nestor.carrasco
 */
public class ContactDaoImpl  extends SqlMapClientDaoSupport  implements ContactDaoIfc {
    @Transactional
    public boolean saveUpdate(Contact contact) {
        SqlMapClientTemplate template = getSqlMapClientTemplate();
        if (contact.getId() == null){
            template.insert("insertContact", contact);
            int groupsCount = (contact.getGroupList() == null)? 0: contact.getGroupList().size();
            Map mapParams = new HashMap();
            for ( int i = 0; i < groupsCount; i ++){
                mapParams.put("id",contact.getId());
                mapParams.put("groupId",contact.getGroupList().get(i));
                mapParams.put("userId",contact.getUserId());                
                template.insert("insertMyContactBookId", mapParams);
            }
            int numberCount = (contact.getNumbers() == null)? 0: contact.getNumbers().size();
            for (int i = 0; i < numberCount; i++){
                ContactNumber cn = contact.getNumbers().get(i);
                    mapParams.put("number",cn.getNumber());
                    mapParams.put("mandatory",cn.getMandatory());
                    mapParams.put("contactId",contact.getId());
                    mapParams.put("reference",cn.getReference());
                    template.insert("insertMyNumbers", mapParams);
            }
            //number save
        }else{
            template.update("updateContact", contact);
             Map mapParams = new HashMap();
             mapParams.put("contactId",contact.getId());
            int numberCount = (contact.getNumbers() == null)? 0: contact.getNumbers().size();
            for (int i = 0; i < numberCount; i++){
                ContactNumber cn = contact.getNumbers().get(i);
                    mapParams.put("number",cn.getNumber());
                    mapParams.put("mandatory",cn.getMandatory());
                    mapParams.put("contactId",contact.getId());
                    mapParams.put("reference",cn.getReference());
                    //Integer existPhone = (Integer) template.queryForObject("searchContactByNumber", mapParams);
                    if ( cn.getId() >= 0){
                    mapParams.put("id",cn.getId());
                    template.update("updateMyNumbers",mapParams);
                    }else{
                   template.insert("insertMyNumbers", mapParams);     
                    }
                        
            }
           
                    
        }
        return true;
    }
    @Transactional(readOnly=true)
    public List<Contact> getContactByUserId(Integer userId) {
        SqlMapClientTemplate template = getSqlMapClientTemplate();
        List<Contact> objectGroup = (List<Contact>) template.queryForList("selectContactsByUserId", userId);
        return objectGroup;
        
    }
    @Transactional(readOnly=true)
    public List<Contact> getContactByGroupId(Integer groupId) {
        SqlMapClientTemplate template = getSqlMapClientTemplate();
        List<Contact> objectGroup = (List<Contact>) template.queryForList("selectContactsByGroupId", groupId);
        return objectGroup;
    }
    @Transactional(readOnly=true)
    public List<Contact> getContactList() {
        SqlMapClientTemplate template = getSqlMapClientTemplate();
        return (List<Contact>)template.queryForList("selectContacts");
    }
    @Transactional(readOnly=true)
    public Contact getContact(Integer contactId) {
        SqlMapClientTemplate template = getSqlMapClientTemplate();
        Contact objContact = (Contact) template.queryForObject("selectContactsById", contactId);
        if ( objContact != null){
            List grpList = (List) template.queryForList("getGroupsAssignedToContact",contactId);
            objContact.setGroupList( grpList);
            List<ContactNumber> numbersList = (List<ContactNumber>) template.queryForList("getNumberFromContactId",contactId);
            objContact.setNumbers(numbersList);
        }
        return objContact;
        
        
    }
    @Transactional(readOnly=true)
    public List<Contact> findContactByName(String name) {
        SqlMapClientTemplate template = getSqlMapClientTemplate();
        Map mapParam = new HashMap();
        mapParam.put("criteria",name+"%");
        return (List<Contact>)template.queryForList("findContacsByName",mapParam);
    }
    @Transactional
    public boolean deleteContact(Integer contactId) {
        SqlMapClientTemplate template = getSqlMapClientTemplate();
        template.delete("deleteGroup", contactId);
        return true;
    }
    @Transactional(readOnly=true)
    public List<ContactNumber> getPhoneNumbers(String[] contactIds){
        SqlMapClientTemplate template = getSqlMapClientTemplate();
        Map mapParam = new HashMap();
        mapParam.put("contactIdSelected",contactIds);
        return (List<ContactNumber>)template.queryForList("getNumberFromContactSelected",mapParam);
    }
    
}
